package com.ebt.m.msgnote;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.commons.utils.BitmapUtil;
import com.ebt.m.msgnote.bean.MsgTypeBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author allen
 * Created by allen on 2018/7/23 18:41.
 */
public class MsgItemView extends IView {

    @BindView(R.id.tv_msg_titleIcon)
    ImageView tvMsgTitleIcon;
    @BindView(R.id.rl_icon_msg)
    RelativeLayout rlIconMsg;
    @BindView(R.id.tv_msg_title)
    TextView tvMsgTitle;
    @BindView(R.id.tv_msg_desc)
    TextView tvMsgDesc;
    @BindView(R.id.tv_msg_label)
    TextView tvMsgLabel;
    @BindView(R.id.tv_msg_arrow)
    ImageView tvMsgArrow;
    @BindView(R.id.tv_msg_divider)
    View tvMsgDivider;
    @BindView(R.id.rlItemViewMsg)
    RelativeLayout rlItemViewMsg;
    MsgTypeBean bean;

    public MsgItemView(Context context) {
        this(context, null);
        ButterKnife.bind(this);
    }

    public MsgItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MsgItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.msg_itemview, this);
    }

    @Override
    public void update(Object... args) {
        bean = (MsgTypeBean) args[0];
        Object extras = args[1];
        if (extras != null && MsgConstant.HIDE_LINE.equals(extras)) {
            tvMsgDivider.setVisibility(GONE);
        } else {
            tvMsgDivider.setVisibility(VISIBLE);
        }
        List<String> labels = bean.getLabel();
        MsgTypeBean.NewlyBean newly = bean.getNewly();
        if (labels == null) {
            tvMsgLabel.setVisibility(INVISIBLE);
        } else {
            StringBuffer label = new StringBuffer();
            int s = labels.size();
            for (int i = 0; i < s; i++) {
                if (i == s - 1) {
                    label.append(labels.get(i));
                } else {
                    label.append(labels.get(i)).append(" ");
                }
            }
            if (s == 0) {
                tvMsgLabel.setVisibility(INVISIBLE);
            } else {
                tvMsgLabel.setText(label);
                tvMsgLabel.setVisibility(VISIBLE);
            }
        }
        int type = bean.getType();
        int unReadCount = bean.getUnReadCount();
        tvMsgTitle.setText(bean.getName());
        if (newly == null || bean.getUnReadCount() == 0) {
            tvMsgDesc.setText("");
            tvMsgDesc.setVisibility(GONE);
        } else {
            tvMsgDesc.setVisibility(VISIBLE);
            tvMsgDesc.setText("您收到新的" + (TextUtils.isEmpty(newly.getTitle()) ? "" : newly.getTitle()) + "提醒");
        }
        switch (type) {
            //消息类型 Customer_Housekeeper(1, "客户管家"),Policy_Remind(2, "保单提醒"),Company_Message(3, "公司消息"),System_Message(4, "系统消息"),Custom_Service(5, "小盈（客服）")
            case 1:
                setIcon(unReadCount, R.drawable.msg_customer_normal);
                break;
            case 2:
                setIcon(unReadCount, R.drawable.msg_order_nomal);
                break;
            case 3:
                setIcon(unReadCount, R.drawable.msg_corp_nomal);
                break;
            case 4:
                setIcon(unReadCount, R.drawable.msg_sys_nomal);
                break;
            case 5:
                setIcon(unReadCount, R.drawable.msg_xiaoying_nomal);
                tvMsgDesc.setText("欢迎使用新版展业家，小盈随时为您服务");
                break;
            default:
                setIcon(unReadCount, R.drawable.msg_sys_nomal);
                break;
        }
    }

    private void setIcon(int unReadCount, int msg_customer_normal) {
        if (unReadCount == 0) {
            tvMsgTitleIcon.setImageResource(msg_customer_normal);
        } else {
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), msg_customer_normal);
            tvMsgTitleIcon.setImageBitmap(BitmapUtil.generatorNumIcon3(getContext(), bmp, true, unReadCount + ""));
        }
    }

    @OnClick(R.id.rlItemViewMsg)
    public void onViewClicked() {
        mOnclickListner.onClick(rlItemViewMsg, bean);
    }
}
